package com.whq.knowverywell.activity;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.whq.knowverywell.R;
import com.whq.knowverywell.adapter.HomeAdapter;
import com.whq.knowverywell.business.model.HomeItem;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.constant.ModelConstant;
import com.whq.knowverywell.util.constant.ParamUtil;

/**
 * 登录后首页
 * @author wanghuaiqiang
 *
 */
public class HomeActivity extends BaseLoginActivity implements OnItemClickListener {
	private  static final String TAG = HomeActivity.class.getSimpleName();
	
	private GridView mGridView;
	private HomeAdapter mAdapter;
	private List<HomeItem> mItemsList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LogUtil.d(TAG, "onCreate start ...");
		setTheme(R.style.Theme_Sherlock_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		
		initData();
		
		mGridView = (GridView) findViewById(R.id.home_gridview);
		mAdapter = new HomeAdapter(this, mItemsList);
		mGridView.setAdapter(mAdapter);
		mGridView.setCacheColorHint(0);
		mGridView.setBackgroundColor(color.transparent);
		mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mGridView.setOnItemClickListener(this);
		
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.top_menu_bg));
		getSupportActionBar().setIcon(R.drawable.home_btn_n);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		
		// 临时代码测试使用
//		new TmpTask(this).execute();
	}
	
	@Override
	protected void onResume() {
		LogUtil.d(TAG, "onResume start ...");
		super.onResume();
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) {
		LogUtil.d(TAG, "onItemClick view id[" + id + "] position[" + position + "]");
		HomeItem item = mAdapter.getItem(position);
		LogUtil.i(TAG, "clicked item is " + item.toString());
		
		if (position == 3) {
			// forward to product search activity
			Intent intent = new Intent();
			intent.setClass(this, ProductSearchActivity.class);
			intent.putExtra(ParamUtil.BUNDLE_KEY_ITEM_ID, item.getItemId());
			startActivity(intent);
		} else {
			// forward to frame activity
			Intent intent = new Intent();
			intent.setClass(this, ContentFrameActivity.class);
			intent.putExtra(ParamUtil.BUNDLE_KEY_ITEM_ID, item.getItemId());
			startActivity(intent);
		}
	}

	@Override
	public void onClick(View v) {
		LogUtil.d(TAG, "onClick view id[" + v.getId() + "]");
		super.onClick(v);
		
		switch (v.getId()) {
//		case R.id.more:
//			TODO
//			break;

		default:
			break;
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SubMenu sub = menu.addSubMenu("设置");
        sub.setIcon(R.drawable.btn_set);
        sub.add(0, BaseLoginActivity.MENU_ITEM_ID_LOGOUT, 0, "注销");
        sub.add(0, BaseLoginActivity.MENU_ITEM_ID_MANAGER_ACCOUNT, 0, "管理帐号");
        sub.add(0, BaseLoginActivity.MENU_ITEM_ID_ABOUT, 0, "关于");
        sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home || item.getItemId() == 0) {
            return false;
        }
        Toast.makeText(this, "Theme changed to \"" + item.getTitle() + "\"", Toast.LENGTH_SHORT).show();
        
        switch (item.getItemId()) {
		case BaseLoginActivity.MENU_ITEM_ID_LOGOUT:
			this.logout();
        	return false;
		case BaseLoginActivity.MENU_ITEM_ID_MANAGER_ACCOUNT:
			this.go2ManagerAccount();
        	return false;
		case BaseLoginActivity.MENU_ITEM_ID_ABOUT:
			
        	return false;
		default:
			break;
		}
        return true;
    }
    
    @Override
    protected void getParameters() {
    	super.getParameters();
    	Intent intent = getIntent();
		if (intent != null) {
			// TODO  这里需要帐户数组信息、零售店的ID
		}
    }
	
	// ------------- customize method -------------
	private void initData() {
		mItemsList = new ArrayList<HomeItem>();
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_SUMMARY, "汇总统计", R.drawable.home_item_summary));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_DOMAIN, "地域分析", R.drawable.home_item_domain));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_SORT, "品类分析", R.drawable.home_item_sort));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_GOODS, "货品查询", R.drawable.home_item_goods));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_HOTSELL, "热销查询", R.drawable.home_item_hotsell));
	}

}